from rest_framework import serializers

from merchants.models import *
from activities.models import *
from equity_admin.serializers import CashActivityDataRetrieveModelSerializer


class MerchantUserRetrieveModelSerializer(serializers.ModelSerializer):

    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)
    is_psbc_card_str = serializers.CharField(source='get_is_psbc_card_display', read_only=True)
    mobile = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = MerchantUser
        fields = ['id', 'mobile', 'openid', 'create_time', 'is_psbc_card', 'is_psbc_card_str', 'lucky_bag']

    def get_mobile(self, obj):
        try:
            mobile = obj.mobile
            return f'{mobile[:3]}****{mobile[-4:]}'
        except:
            return ''


class LuckyBagActivityListModelSerializer(serializers.ModelSerializer):

    available_begin_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)
    available_end_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)
    activity_status_str = serializers.CharField(source='get_activity_status_display', read_only=True)

    class Meta:
        model = LuckyBagActivity
        fields = ['id', 'activity_name', 'activity_status', 'activity_status_str', 'available_begin_time', 'available_end_time', 'description', 'explain']


class LuckyBagChildActivityListModelSerializer(serializers.ModelSerializer):

    activity_type_str = serializers.CharField(source='get_activity_type_display', read_only=True)

    class Meta:
        model = LuckyBagChildActivity
        fields = ['id', 'activity_type', 'activity_type_str', 'description', 'limit_time_description', 'limit_day_description', 'banner']


class LuckyBagActivityRetrieveModelSerializer(serializers.ModelSerializer):

    available_begin_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)
    available_end_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)
    activity_status_str = serializers.CharField(source='get_activity_status_display', read_only=True)
    child_set = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = LuckyBagActivity
        fields = ['id', 'activity_name', 'activity_status', 'activity_status_str', 'available_begin_time', 'available_end_time', 'description', 'explain', 'child_set']

    def get_child_set(self, obj):
        child_set = obj.luckybagchildactivity_set.all()
        return LuckyBagChildActivityListModelSerializer(child_set, many=True).data


class LuckyBagChildActivityDataListModelSerializer(serializers.ModelSerializer):

    received_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)
    lucky_status_str = serializers.CharField(source='get_lucky_status_display', read_only=True)
    activity_type_str = serializers.SerializerMethodField(read_only=True)
    activity_type = serializers.SerializerMethodField(read_only=True)
    mobile = serializers.SerializerMethodField(read_only=True)
    level_data = serializers.SerializerMethodField(read_only=True)
    merchant_data = serializers.SerializerMethodField(read_only=True)
    lucky_bag = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = LuckyBagChildActivityData
        fields = ['id', 'business_no', 'lucky_bag', 'lucky_status_str', 'lucky_status', 'received_time', 'activity_type_str', 'luckybagchildactivity_id',
                  'mobile', 'merchantuser_id', 'activity_type', 'level_data', 'merchant_data']

    def get_lucky_bag(self, obj):
        if obj.lucky_status == 2:
            return obj.lucky_bag
        else:
            return ''

    def get_level_data(self, obj):
        data = {}
        data['name'] = ''
        data['account'] = ''
        try:
            level = obj.level
            data['name'] = level.name
            data['account'] = level.account
        except:
            pass
        return data

    def get_merchant_data(self, obj):
        data = {}
        data['id'] = ''
        data['business_no'] = ''
        data['name'] = ''
        try:
            merchant = Merchant.objects.get(wyfMerchantID=obj.business_no)
            data['id'] = merchant.id
            data['business_no'] = merchant.wyfMerchantID
            data['name'] = merchant.name
        except:
            pass
        return data

    def get_activity_type_str(self, obj):
        return obj.luckybagchildactivity.get_activity_type_display()

    def get_activity_type(self, obj):
        return obj.luckybagchildactivity.activity_type

    def get_mobile(self, obj):
        try:
            mobile = obj.merchantuser.mobile
            return f'{mobile[:3]}****{mobile[-4:]}'
        except:
            return ''


class LuckyBagChildActivityDataRetrieveModelSerializer(serializers.ModelSerializer):

    received_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)
    lucky_status_str = serializers.CharField(source='get_lucky_status_display', read_only=True)
    activity_type_str = serializers.SerializerMethodField(read_only=True)
    activity_type = serializers.SerializerMethodField(read_only=True)
    mobile = serializers.SerializerMethodField(read_only=True)
    level_data = serializers.SerializerMethodField(read_only=True)
    merchant_data = serializers.SerializerMethodField(read_only=True)
    lucky_bag = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = LuckyBagChildActivityData
        fields = ['id', 'business_no', 'lucky_bag', 'lucky_status_str', 'lucky_status', 'received_time', 'activity_type_str', 'luckybagchildactivity_id',
                  'mobile', 'merchantuser_id', 'activity_type', 'level_data', 'merchant_data']

    def get_lucky_bag(self, obj):
        if obj.lucky_status == 2:
            return obj.lucky_bag
        else:
            return ''

    def get_level_data(self, obj):
        data = {}
        data['name'] = ''
        data['account'] = ''
        try:
            level = obj.level
            data['name'] = level.name
            data['account'] = level.account
        except:
            pass
        return data

    def get_merchant_data(self, obj):
        data = {}
        data['id'] = ''
        data['business_no'] = ''
        data['name'] = ''
        try:
            merchant = Merchant.objects.get(wyfMerchantID=obj.business_no)
            data['id'] = merchant.id
            data['business_no'] = merchant.wyfMerchantID
            data['name'] = merchant.name
        except:
            pass
        return data

    def get_activity_type_str(self, obj):
        return obj.luckybagchildactivity.get_activity_type_display()

    def get_activity_type(self, obj):
        return obj.luckybagchildactivity.activity_type

    def get_mobile(self, obj):
        try:
            mobile = obj.merchantuser.mobile
            return f'{mobile[:3]}****{mobile[-4:]}'
        except:
            return ''


class LuckyBagRecordListModelSerializer(serializers.ModelSerializer):

    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)
    operate_type_str = serializers.CharField(source='get_operate_type_display', read_only=True)
    mobile = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = LuckyBagRecord
        fields = ['id', 'lucky_bag', 'operate_type_str', 'operate_type', 'create_time', 'lucky_bag_balance',
                  'mobile', 'merchantuser_id']

    def get_mobile(self, obj):
        try:
            mobile = obj.merchantuser.mobile
            return f'{mobile[:3]}****{mobile[-4:]}'
        except:
            return ''


class LuckyBagRecordRetrieveModelSerializer(serializers.ModelSerializer):

    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)
    operate_type_str = serializers.CharField(source='get_operate_type_display', read_only=True)
    mobile = serializers.SerializerMethodField(read_only=True)
    cashactivitydata = serializers.SerializerMethodField(read_only=True)
    activitydata = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = LuckyBagRecord
        fields = ['id', 'lucky_bag', 'operate_type_str', 'operate_type', 'create_time', 'lucky_bag_balance',
                  'mobile', 'merchantuser_id', 'cashactivitydata', 'activitydata']

    def get_mobile(self, obj):
        try:
            mobile = obj.merchantuser.mobile
            return f'{mobile[:3]}****{mobile[-4:]}'
        except:
            return ''

    def get_cashactivitydata(self, obj):
        try:
            if obj.operate_type == 2:
                cashactivitydata = obj.cashactivitydata
                return CashActivityDataRetrieveModelSerializer(cashactivitydata).data
            else:
                return {}
        except:
            return {}

    def get_activitydata(self, obj):
        try:
            if obj.operate_type == 1:
                activitydata = obj.activitydata
                return LuckyBagChildActivityDataRetrieveModelSerializer(activitydata).data
            else:
                return {}
        except:
            return {}


class MerchantNFCDeviceTradeModelSerializer(serializers.ModelSerializer):

    trade_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)
    merchant = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = MerchantNFCDeviceTrade
        fields = ('id', 'sn', 'device_type', 'store_name', 'transaction_id', 'total_amount', 'trade_time', 'merchant')

    def get_merchant(self, obj):
        data = {}
        data['merchant_name'] = ''
        data['business_no'] = ''
        data['city_name'] = ''
        data['district_name'] = ''
        data['branch_name'] = ''
        try:
            merchant = obj.merchant
            if merchant:
                data['merchant_name'] = merchant.name
                data['business_no'] = merchant.wyfMerchantID
                level = merchant.level
                data['city_name'] = level.city_name
                data['district_name'] = level.district_name
                data['branch_name'] = level.branch_name
        except:
            pass
        return data
